package com.junzhiit.nengji.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.OrdersDataHistoryBO;
import com.junzhiit.nengji.bo.WithdrawalHistoryBO;
import com.junzhiit.nengji.cms.service.WithdrawalHistoryService;
import com.junzhiit.nengji.cms.web.exception.MobilePhoneCodeErrorException;
import com.junzhiit.nengji.cms.web.exception.MobilePhoneCodeTimeoutException;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.exception.WithdrawalMoneyOverException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.util.tools.NumberUtils;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.OrdersDataHistoryVO;
import com.junzhiit.nengji.cms.web.vo.PhoneCodeVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.cms.web.vo.WithdrawalHistoryVO;
import com.junzhiit.nengji.dao.GoodsDataHistoryDAO;
import com.junzhiit.nengji.dao.OrdersDataHistoryDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.WithdrawalHistoryDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.WithdrawalHistory;
import com.junzhiit.nengji.model.enums.BankCardType;
import com.junzhiit.nengji.model.enums.LoginType;
import com.junzhiit.nengji.model.enums.Status;
import com.junzhiit.nengji.model.enums.WithdrawalStatus;

@Service
public class WithdrawalHistoryServiceImpl extends BaseServiceImpl implements WithdrawalHistoryService {

	@Autowired
	private WithdrawalHistoryDAO withdrawalHistoryDAO;

	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private OrdersDataHistoryDAO ordersDataHistoryDAO;
	
	@Autowired
	private GoodsDataHistoryDAO goodsDataHistoryDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, WithdrawalHistoryVO shopWithdrawalHistoryVO) {
		WithdrawalHistory shopWithdrawalHistory = new WithdrawalHistory();
		BeanUtils.copyProperties(shopWithdrawalHistoryVO, shopWithdrawalHistory);

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(shopWithdrawalHistoryVO.getSessionId()), SystemUserSessionVO.class);

		if (StringUtils.isNotBlank(systemUserSessionVO.getSystemId())) {
			shopWithdrawalHistory.setShopId(systemUserSessionVO.getSystemId());
		}

		List<WithdrawalHistoryBO> list = withdrawalHistoryDAO.findAll(shopWithdrawalHistory, dtp.getStart(),
				dtp.getLength());
		int totalCount = withdrawalHistoryDAO.getTotalCount(shopWithdrawalHistory);

		if (shopWithdrawalHistoryVO.getState() == Status.ACTIVE.toIntStatus()) {
			for (WithdrawalHistoryBO history : list) {
				if (StringUtils.isNotBlank(history.getBankCard())) {
					history.setBankCard(history.getBankCard().substring(0, 4) + " **** **** *** "
							+ history.getBankCard().substring(15, 19));
				}
			}
		}

		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO insertHistory(WithdrawalHistoryVO shopWithdrawalHistoryVO) {
		ResultVO result = new ResultVO();
		WithdrawalHistory shopWithdrawalHistory = new WithdrawalHistory();
		// 验证验证码
		PhoneCodeVO phoneCode = systemUserCacheDAO
				.getHash(DIYKey.getMobilePhoneCodeKey(shopWithdrawalHistoryVO.getSendPhone()), PhoneCodeVO.class);
		if (!phoneCode.getCode().equals(shopWithdrawalHistoryVO.getCode())) {
			throw new MobilePhoneCodeErrorException("验证码不一致");
		}

		if (phoneCode.getTimestamp() < new Date().getTime()) {
			throw new MobilePhoneCodeTimeoutException("验证码已过期");
		}

		BeanUtils.copyProperties(shopWithdrawalHistoryVO, shopWithdrawalHistory);
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(shopWithdrawalHistoryVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());

		if (shopWithdrawalHistory.getWithdrawalMoney() != null
				&& shop.getWithdrawalMoney() >= shopWithdrawalHistory.getWithdrawalMoney()) {
			shop.setWithdrawalMoney(shop.getWithdrawalMoney() - shopWithdrawalHistory.getWithdrawalMoney());
			shopDAO.update(shop);
		} else {
			throw new WithdrawalMoneyOverException("提现金额超出可提现金额！");
		}
		shopWithdrawalHistory.setShopId(systemUserSessionVO.getSystemId());
		shopWithdrawalHistory.setApplyTime(new Date());
		shopWithdrawalHistory.setStatus(WithdrawalStatus.WAIT.toIntValue());
		shopWithdrawalHistory.setWithdrawalNumber(NumberUtils.getOrderlNumber());
		shopWithdrawalHistory.setType(BankCardType.SHOP.toIntValue());
		withdrawalHistoryDAO.insert(shopWithdrawalHistory);
		return result;
	}

	@Override
	public ResultVO update(WithdrawalHistoryVO WithdrawalHistoryVO) {
		ResultVO result = new ResultVO();
		WithdrawalHistory withdrawalHistory = withdrawalHistoryDAO.query(WithdrawalHistoryVO.getId());
		Shop shop = shopDAO.query(withdrawalHistory.getShopId());
		withdrawalHistory.setStatus(WithdrawalHistoryVO.getStatus());
		withdrawalHistory.setConfirmType(WithdrawalHistoryVO.getConfirmType());
		if (WithdrawalHistoryVO.getStatus() == WithdrawalStatus.WAITING.toIntValue()) {
			withdrawalHistory.setDisposeTime(new Date());
		} else if (WithdrawalHistoryVO.getStatus() == WithdrawalStatus.FAIL.toIntValue()) {
			shop.setWithdrawalMoney(withdrawalHistory.getWithdrawalMoney() + shop.getWithdrawalMoney());
			withdrawalHistory.setDisposeTime(new Date());
			withdrawalHistory.setCompleteTime(new Date());
			withdrawalHistory.setFailReason(WithdrawalHistoryVO.getFailReason());
			shopDAO.update(shop);
		} else if (WithdrawalHistoryVO.getStatus() == WithdrawalStatus.SUCCESS.toIntValue()) {
			withdrawalHistory.setCompleteTime(new Date());
		}
		withdrawalHistoryDAO.update(withdrawalHistory);
		return result;
	}

	@Override
	public int getWaitAuditCount() {
		return withdrawalHistoryDAO.getWaitAuditCount();
	}
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, OrdersDataHistoryVO ordersDataHistoryVO) {
		String key = DIYKey.getSessionHashKey(ordersDataHistoryVO.getSessionId());
		SystemUserSessionVO session = systemUserCacheDAO.getHash(key, SystemUserSessionVO.class);
		String shopId=null;
		if (LoginType.SHOP.toIntType() == session.getLoginType()) {
			shopId=session.getSystemId();
		}
		Shop shop=shopDAO.query(shopId);
		if(shop==null){
			throw new ShopNotFoundException("店铺没找到");
		}
		OrdersDataHistoryBO ordersDataHistoryBO = new OrdersDataHistoryBO();
		BeanUtils.copyProperties(ordersDataHistoryVO, ordersDataHistoryBO);
		ordersDataHistoryBO.setShopId(shop.getId());
		List<OrdersDataHistoryBO> list= ordersDataHistoryDAO.find(ordersDataHistoryBO,dtp.getStart(), dtp.getLength());
		int totalCount = ordersDataHistoryDAO.totalCount(ordersDataHistoryBO);

		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO updateMonthIncomeMoney() {
		ResultVO result=new ResultVO();
		Double money=0.00;
		List<Shop> shops = shopDAO.findAll();
		for(Shop shop : shops){
			shop.setMonthIncomeMoney(money);
			shopDAO.update(shop);
		}
		return result;
	}

	@Override
	public ResultVO shopFindGoods(String ordersDataHistoryId) {
		ResultVO result=new ResultVO();
		result.setData(goodsDataHistoryDAO.shopFindGoods(ordersDataHistoryId));
		return result;
	}

}
